package com.ruoyi.api.v1;

import com.ruoyi.cinema.BO.GetEffectivePromoBO;
import com.ruoyi.cinema.DTO.DeliverGoodsRetYzDTO;
import com.ruoyi.cinema.DTO.EffectivePromoGoodsYzDTO;
import com.ruoyi.cinema.domain.Cinema;
import com.ruoyi.cinema.domain.Customer;
import com.ruoyi.cinema.domain.Goods;
import com.ruoyi.cinema.domain.GoodsCategory;
import com.ruoyi.cinema.service.ICinemaService;
import com.ruoyi.cinema.service.ICustomerService;
import com.ruoyi.cinema.service.IGoodsCategoryService;
import com.ruoyi.cinema.service.IGoodsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.utils.YzResult;
import com.ruoyi.utils.YzUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("v1_Goods")
@RequestMapping(value = "api/v1/goods")
public class GoodsController  extends BaseController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsCategoryService goodsCategoryService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICinemaService cinemaService;

    @GetMapping("/category")
    public AjaxResult getGoodsCategory(GoodsCategory goodsCategory){
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
        return AjaxResult.success(goodsCategoryList);
    }


    @GetMapping("/list")
    public TableDataInfo list(Goods goods) {
        if(null == goods.getCinemaId()){
            return getDataTableError(-1,"cinemaId 不能为空");
        }
        if(null == goods.getCustomerId()){
           return getDataTableError(-1,"customerId 不能为空");
        }
        Cinema cinema = cinemaService.selectCinemaById(goods.getCinemaId());
        Customer customer = customerService.selectCustomerById(goods.getCustomerId());
        String accessToken = customerService.getAccessToken(customer.getId(),customer.getPhone());
        if(StringUtils.isEmpty(accessToken)){
            return getDataTableError(-1,"获取accessToken失败，请绑定手机号后重试");
        }
        startPage();
        //筛选库存为0的商品
        goods.setGoodsStorageCount(0L);
        List<Goods> list = goodsService.selectGoodsListForApi(goods,cinema.getCinemaLinkId(),accessToken);
        return getDataTable(list);
    }

    @PostMapping("/getGoodsCanUsePrice")
    public AjaxResult getGoodsCanUsePrice(@RequestBody GetEffectivePromoBO gepb){
        if(StringUtils.isEmpty(gepb.getCustomerId())){
            return AjaxResult.error(-1,"customerId 不能为空");
        }
        Customer customer = customerService.getById(gepb.getCustomerId());
        String accessToken = customerService.getAccessToken(customer.getId(),customer.getPhone());
        if(StringUtils.isEmpty(accessToken)){
            return AjaxResult.error(-1,"获取accessToken失败，请绑定手机号后重试");
        }
        if(null == gepb.getGoodsList()){
            return AjaxResult.error(-1,"商品列表不能为空");
        }
        gepb.setTicketList(null);
        for (EffectivePromoGoodsYzDTO egp  :gepb.getGoodsList()) {
            if(!egp.getGoodsCount().equals(1)){
                return AjaxResult.error(-1,"卖品数量必须为1");
            }
        }
        gepb.setAccessToken(accessToken);
        gepb.setCustomer(customer);
        return AjaxResult.success(goodsService.getCanUsePrice(gepb));
    }

    @GetMapping("/getDeliverGoodsDate")
    public AjaxResult getDeliverGoodsDate(GetEffectivePromoBO gepb){
        if(StringUtils.isEmpty(gepb.getCustomerId())){
            return AjaxResult.error(-1,"customerId 不能为空");
        }
        if(StringUtils.isEmpty(gepb.getCinemaLinkId())){
            return AjaxResult.error(-1,"cinemaLinkId不能为空");
        }
        Customer customer = customerService.getById(gepb.getCustomerId());
        String accessToken = customerService.getAccessToken(customer.getId(),customer.getPhone());
        YzResult<DeliverGoodsRetYzDTO> retYzDTOYzResult = YzUtils.getDeliverGoodsDateInfo(gepb.getCinemaLinkId(),null,accessToken);
        return AjaxResult.success(retYzDTOYzResult);
    }


}
